package com.ruoyi.web.controller.material.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.common.core.domain.entity.SysDictType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.controller.material.entity.request.SupplierAddRequest;
import com.ruoyi.web.controller.material.entity.request.SupplierPageRequest;
import com.ruoyi.web.controller.material.entity.ylSupplier;
import com.ruoyi.web.controller.material.mapper.SupplierMapper;
import com.ruoyi.web.controller.material.service.SupplierService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 供应商Service业务层处理
 *
 * @author jzx
 * @date 2025年10月21日16:01:44
 */
@Service
public class SupplierServiceImpl implements SupplierService {

    @Resource
    private SupplierMapper supplierMapper;

    /**
     * 列表查询
     */
    @Override
    public List<ylSupplier> getPage(SupplierPageRequest supplierPageRequest) {
        LambdaQueryWrapper<ylSupplier> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(supplierPageRequest.getSupplierName())) {
            queryWrapper.like(ylSupplier::getSupplierName, supplierPageRequest.getSupplierName());
        }
        if (StringUtils.isNotEmpty(supplierPageRequest.getContactPhone())) {
            queryWrapper.eq(ylSupplier::getContactPhone, supplierPageRequest.getContactPhone());
        }
        queryWrapper.orderByDesc(ylSupplier::getCreateTime);
        return supplierMapper.selectList(queryWrapper);
    }

    /**
     * 删除
     */

    @Override
    public void delete(Long[] id) {
        for (Long dictId : id) {
            supplierMapper.deleteById(dictId);
        }
    }

    /**
     * 新增供应商
     */
    @Override
    public void add(SupplierAddRequest supplierAddRequest) {

        if (supplierMapper.getWarehouseName(supplierAddRequest.getSupplierName(), null)) {
            throw new ServiceException("供应商名称已存在");
        }
        ylSupplier ylSupplier = new ylSupplier();
        ylSupplier.setSupplierName(supplierAddRequest.getSupplierName());
        ylSupplier.setContactPerson(supplierAddRequest.getContactPerson());
        ylSupplier.setContactPhone(supplierAddRequest.getContactPhone());
        ylSupplier.setEmail(supplierAddRequest.getEmail());
        ylSupplier.setAddress(supplierAddRequest.getAddress());
        ylSupplier.setRemark(supplierAddRequest.getRemark());
        supplierMapper.insert(ylSupplier);
    }

    /**
     * 根据id查询
     */
    @Override
    public ylSupplier selectById(Long id) {
        return supplierMapper.selectById(id);
    }

    /**
     * 修改供应商
     */
    @Override
    public void update(SupplierAddRequest supplierAddRequest) {

        if (supplierMapper.getWarehouseName(supplierAddRequest.getSupplierName(), String.valueOf(supplierAddRequest.getId()))) {
            throw new ServiceException("供应商名称已存在");
        }
        supplierMapper.update(
                null, // entity 为 null，表示不靠实体更新
                new LambdaUpdateWrapper<ylSupplier>()
                        .eq(ylSupplier::getId, supplierAddRequest.getId())
                        .set(ylSupplier::getSupplierName, supplierAddRequest.getSupplierName())
                        .set(ylSupplier::getContactPerson, supplierAddRequest.getContactPerson())
                        .set(ylSupplier::getContactPhone, supplierAddRequest.getContactPhone())
                        .set(ylSupplier::getEmail, supplierAddRequest.getEmail())
                        .set(ylSupplier::getAddress, supplierAddRequest.getAddress())
                        .set(ylSupplier::getRemark, supplierAddRequest.getRemark())
        );
    }
}
